//
//  FullScreenMode.m
//  X-Touch2
//
//  Created by shengchao yang on 12-3-8.
//  Copyright (c) 2012年 foxhis. All rights reserved.
//

#import "FullScreenMode.h"
#import "PopoverPickerviewController.h"
#import "Singleton.h"
#import "Imports1.h"
#import "DataBaseAccess.h"

#define animateTime 0.5f   //动画切换时间间隔

@interface FullScreenMode(Private)
- (void) add1fingerTap;
- (void) add1fingerLeftSwipe;
- (void) add1fingerRightSwipe;
- (void) addPinch;

- (void) nextPage;
- (void) previousPage;
- (void) refreshData;
- (BOOL) checkIndexRange:(BOOL) nextOrPrevious;
-(void) setRemark:(PluPlu *)temp;
-(void) setPriceSelected:(NSInteger)priceIndex;
-(void) setPluName:(NSString *)ppDescripts pluCode:(NSString *)plucode;
@end

@implementation FullScreenMode

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {        
        //给界面里的控件赋予指针变量
        viewPluName = [self.view viewWithTag:TagOfUIView];
        viewRemark = [self.view viewWithTag:TagOfUIView+1];
        [viewRemark setClipsToBounds:YES];
        viewCook = [self.view viewWithTag:TagOfUIView+2];
        viewCook.frame = CGRectMake(viewPluName.frame.origin.x, 
                                    viewRemark.frame.origin.y+viewRemark.frame.size.height+10, 
                                    viewRemark.frame.size.width, 
                                    150);
        viewControl = [self.view viewWithTag:TagOfUIView+3];
        
        imgDish = (UIImageView *)[self.view viewWithTag:TagOfUIImageView];//全屏菜品图片
        imgMark = (UIImageView *)[self.view viewWithTag:TagOfUIImageView+1];//是否已点的打钩图片
        
        imgDirection = (UIImageView *)[viewPluName viewWithTag:TagOfUIImageView+2];//方向指示箭头
        lblPluName = (UILabel *)[viewPluName viewWithTag:TagOfUILabel];
        
        textViewRemark = (UITextView *)[viewRemark viewWithTag:TagOfUITextView];
        lblCook = (UILabel *)[viewCook viewWithTag:TagOfUILabel+1];
        lblCookName = (UILabel * )[viewCook viewWithTag:TagOfUILabel + 5];//“烧法” 翻译用
        lblCookName.text = TranslateStr(@"烧法");
        
        lblPrice = (UILabel *)[viewControl viewWithTag:TagOfUILabel+2];
        lblNum = (UILabel *)[viewControl viewWithTag:TagOfUILabel+3];
        lblNum.text = TranslateStr(@"数量");
        btnNum = (UIButton *)[viewControl viewWithTag:TagOfUIButton+10];
        
        btnOrder = (UIButton *)[viewControl viewWithTag:TagOfUIButton+11];
        [btnOrder setTitle:TranslateStr(@"点单") forState:UIControlStateNormal];
        btnCook = (UIButton *)[viewControl viewWithTag:TagOfUIButton+12];
        [btnCook setTitle:TranslateStr(@"烧法") forState:UIControlStateNormal];
        btnReturn = (UIButton *)[viewControl viewWithTag:TagOfUIButton+13];
        [btnReturn setTitle:TranslateStr(@"返回") forState:UIControlStateNormal];
        
        [self add1fingerTap];
        [self add1fingerLeftSwipe];
        [self add1fingerRightSwipe];
//        [self addPinch];
        
        [self refreshData];//刷新界面数据
    }
    return self;
}

#pragma mark - 添加手势
//手指单击 只对菜品名称view有效！！！
- (void) add1fingerTap{
	UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapFrom:)];
    recognizer.numberOfTouchesRequired = 1;//多少根手指
    recognizer.numberOfTapsRequired = 1;//点击几次
	[viewPluName addGestureRecognizer:recognizer];//只对菜品名称view有效！！！
	[recognizer release];	
}
//手指从右滑向左
- (void) add1fingerLeftSwipe{
	UISwipeGestureRecognizer *recognizer1;
	recognizer1 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeLeft:)];
	recognizer1.direction = UISwipeGestureRecognizerDirectionLeft;
	[self.view addGestureRecognizer:recognizer1];
	[recognizer1 release];
}
//手指从左滑向右
- (void) add1fingerRightSwipe{
	UISwipeGestureRecognizer *recognizer1;
	recognizer1 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeRight:)];
	recognizer1.direction = UISwipeGestureRecognizerDirectionRight;
	[self.view addGestureRecognizer:recognizer1];
	[recognizer1 release];
}
//用手指捏 返回
- (void) addPinch{                                                                                                   
	UIPinchGestureRecognizer *recognizer1;
	recognizer1 = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinchForm:)];
	[self.view addGestureRecognizer:recognizer1];
	[recognizer1 release];	
}

#pragma mark - 子view的下拉和隐藏
-(void) startAnimate{
    AppSession.isAnimating = YES;
}
-(void) movingIn{
    AppSession.isAnimating = NO;
    viewRemark.hidden = NO;
    imgDirection.image = [FileManager getImage:@"arrow_down_fullscreen.png" type:3 withRect:imgDirection.frame];
    viewCook.frame = CGRectMake(viewPluName.frame.origin.x, 
                                viewRemark.frame.origin.y+viewRemark.frame.size.height+10, 
                                viewRemark.frame.size.width, 
                                150);
}
-(void) movingOut{
    AppSession.isAnimating = NO;
    viewRemark.hidden=YES;
    imgDirection.image = [FileManager getImage:@"arrow_up_fullscreen.png" type:3 withRect:imgDirection.frame];
    viewCook.frame = CGRectMake(viewPluName.frame.origin.x, 
                                viewPluName.frame.origin.y+viewPluName.frame.size.height+10, 
                                viewPluName.frame.size.width, 
                                150);
}

#pragma mark - 左右翻页手势delegate
//点击菜品所在的UIView触发
- (void) handleTapFrom:(UITapGestureRecognizer *)recognizer {
    MyNSLog(@"点击了菜品名称...");
    if (AppSession.isAnimating) {
        MyNSLog(@"上一个动画还未结束...");
        return;
    }
    [AppSession playMusic:VoiceFile(@"clicked.caf")];
    if (isRemarkDisplay) {
        [textViewRemark slideOutTo:kFTAnimationTop inView:imgDish  duration:0.2f delegate:self startSelector:@selector(startAnimate) stopSelector:@selector(movingOut)];
    }else {
        [textViewRemark slideInFrom:kFTAnimationTop inView:imgDish  duration:0.2f delegate:self startSelector:@selector(startAnimate) stopSelector:@selector(movingIn)];
    }
    isRemarkDisplay = !isRemarkDisplay;
}
//看后面(右)的内容
- (void)handleSwipeLeft:(UISwipeGestureRecognizer *)recognizer {
    [self nextPage];
}
//看前面(左)的内容
- (void)handleSwipeRight:(UISwipeGestureRecognizer *)recognizer {
    [self previousPage];
}
//用手指捏 退出全屏模式
- (void)handlePinchForm:(UIPinchGestureRecognizer *)recognizer{
    if (recognizer.scale < 0.3 && recognizer.enabled) {//手指捏的距离大于70%才触发
        [AppSession playMusic:VoiceFile(@"trash.caf")];
        recognizer.enabled=NO;//首先关闭手势        
        NSString *SysMsg = [NSString stringWithFormat:@"SysMsg_MessageBoxBaseView_%@", self.nibName];
        postN(SysMsg);
    }
}

#pragma mark - 核心部分！界面刷新
//刷新界面数据(左右翻页和初始化时调用)
-(void) refreshData{
    isRemarkDisplay = NO;
    if (AppSession.fullScreenType == 0) {//推荐菜大图
        _currentPlu = (PluPlu *)[AppSession.commendPluPlus objectAtIndex:AppSession.commendIndex];
        [self setPriceSelected:0];
        [imgDish setImage:[FileManager getImage:_currentPlu.ppImage type:0 withRect:imgDish.frame]];
        [self setPluName:_currentPlu.ppDescripts pluCode:_currentPlu.ppCode];
        [self setRemark:_currentPlu];
    }else if(AppSession.fullScreenType == 1){//正式点菜大图
        _currentPlu = (PluPlu *)[AppSession.orderingPluPlus objectAtIndex:AppSession.pluIndex];
        [self setPriceSelected:0];
        [imgDish setImage:[FileManager getImage:_currentPlu.ppImage type:0 withRect:imgDish.frame]];
        [self setPluName:_currentPlu.ppDescripts pluCode:_currentPlu.ppCode];
        [self setRemark:_currentPlu];
    }else if(AppSession.fullScreenType == 2){//我的点单菜品大图
        _currentMyOrderDish = (MyOrderDishes *)[AppSession.myOrderDishes objectAtIndex:AppSession.myOrderPluIndex];
        [lblPrice setText:PriceFormatter(_currentMyOrderDish.modPrice, JsonField(_currentMyOrderDish.modUnits))];
        [imgDish setImage:[FileManager getImage:_currentMyOrderDish.modPluPlu.ppImage type:0 withRect:imgDish.frame]];
        [self setPluName:_currentMyOrderDish.modDescripts pluCode:_currentMyOrderDish.ppCode];
        [btnNum setTitle:_currentMyOrderDish.modNumber forState:UIControlStateNormal];
        
        if ([_currentMyOrderDish.modNumber floatValue] > 0) {
            [btnNum setTitleColor:[UIColor redColor] forState:UIControlStateNormal];//已点单且数量不为0时显示红色字体
        }else {
            [btnNum setTitleColor:RGBA(163, 163, 163, 255) forState:UIControlStateNormal];//数量为0显示灰色字体
        }
        
        [self checkCookViewDisplay];
        [self setRemark:_currentMyOrderDish.modPluPlu];
    }else if(AppSession.fullScreenType == 3 || AppSession.fullScreenType == 4){//浏览查询结果的菜品对象
        _currentPlu = (PluPlu *)[AppSession.searchPlus objectAtIndex:AppSession.searchIndex];
        [self setPriceSelected:0];
        [imgDish setImage:[FileManager getImage:_currentPlu.ppImage type:0 withRect:imgDish.frame]];
        [self setPluName:_currentPlu.ppDescripts pluCode:_currentPlu.ppCode];
        [self setRemark:_currentPlu];
    }
    
    //------------这里设置在新增套餐明细时不能选烧法，否则业务逻辑太复杂不好维护-----------
    if(AppSession.fullScreenType == 4){
        btnCook.hidden = YES;
    }else {
        btnCook.hidden = NO;
    }
    //------------END--------------
}
//设置菜名
-(void) setPluName:(NSString *)ppDescripts pluCode:(NSString *)plucode{
    if ([AppSession.language isContainString:@"zh-Han"]) {//中文条件下要求显示菜品代码
        [lblPluName setText:[NSString stringWithFormat:@"[%@]%@",plucode,JsonField(ppDescripts)]];  
    }else {
        [lblPluName setText:JsonField(ppDescripts)];  
    }
}
//显示菜品备注信息
-(void) setRemark:(PluPlu *)temp{
    if ([temp.ppType isEqualToString:@"0"]) {//食品
        if (temp.ppFood != nil) {
            textViewRemark.text = [NSString stringWithFormat:@"%@：%@ \r\n%@：%@ \r\n%@：%@\r\n",
                                   TranslateStr(@"烧法"), 
                                   JsonField(temp.ppFood.pfCooks),
                                   TranslateStr(@"材料"), 
                                   JsonField(temp.ppFood.pfMeterials),
                                   TranslateStr(@"备注"), 
                                   JsonField(temp.ppFood.pfRemarks)];
        }else {
            textViewRemark.text = @"";
        }
    }else if ([temp.ppType isEqualToString:@"1"]) {//香烟
        if (temp.ppCigarete != nil) {
            textViewRemark.text = [NSString stringWithFormat:@"%@：%@ \r\n%@：%@ \r\n%@：%@ \r\n%@：%@ \r\n%@：%@\r\n",
                                   TranslateStr(@"Nicotine"), 
                                   JsonField(temp.ppCigarete.pcNicotine),
                                   TranslateStr(@"pcPackages"), 
                                   JsonField(temp.ppCigarete.pcPackages),
                                   TranslateStr(@"pcTar"), 
                                   JsonField(temp.ppCigarete.pcTar),
                                   TranslateStr(@"pcCO"), 
                                   JsonField(temp.ppCigarete.pcCO),
                                   TranslateStr(@"备注"), 
                                   JsonField(temp.ppCigarete.pcRemarks)];
        }else {
            textViewRemark.text = @"";
        }
    }else if ([temp.ppType isEqualToString:@"2"]){//酒水
        if (temp.ppDrink != nil) {
            textViewRemark.text = [NSString stringWithFormat:@"%@：%@ \r\n%@：%@ \r\n%@：%@ \r\n%@：%@ \r\n%@：%@\r\n%@：%@\r\n",
                                   TranslateStr(@"容量"), 
                                   JsonField(temp.ppDrink.pdCapacity),
                                   TranslateStr(@"产地"), 
                                   JsonField(temp.ppDrink.pdPlaces),
                                   TranslateStr(@"酒精度"), 
                                   JsonField(temp.ppDrink.pdPrecision),
                                   TranslateStr(@"最适温度"), 
                                   JsonField(temp.ppDrink.pdTempereature),
                                   TranslateStr(@"年份"), 
                                   JsonField(temp.ppDrink.pdYear),
                                   TranslateStr(@"备注"), 
                                   JsonField(temp.ppDrink.pdRemarks)];
        }else {
            textViewRemark.text = @"";
        }
    }else if ([temp.ppType isEqualToString:@"3"]){//套菜
        NSString * strRemark=@"套餐明细：\r\n";
        for (int index=0; index < temp.ppPluPkgs.count; index++) {
            MyOrderPluPkgs *tempPluPkg = (MyOrderPluPkgs *)[temp.ppPluPkgs objectAtIndex:index];
            strRemark = [strRemark stringByAppendingFormat:@"%d. %@\r\n", (index+1), JsonField(tempPluPkg.pkgDescripts)];
        }
        textViewRemark.text = strRemark;    
    }
}

//按下多价格的单位按钮
//参数：按钮的index
-(void) setPriceSelected:(NSInteger)priceIndex{
    if (_currentPlu.ppPluPrices == nil || [_currentPlu.ppPluPrices count] == 0) {
        [lblPrice setText:PriceFormatter(0,nil)];
    }else{
        _currentPluPrice = (PluPrice *)[_currentPlu.ppPluPrices objectAtIndex:priceIndex];
        for (int i=0; i<5; i++) {
            UIButton *btnUnit = (UIButton *)[viewControl viewWithTag:TagOfUIButton + i];
            if (i < [_currentPlu.ppPluPrices count]) {
                PluPrice *tempPluPrice = (PluPrice *)[_currentPlu.ppPluPrices objectAtIndex:i];
                
                //设置单位按钮背景图片和单位名称
                [btnUnit setTitle:JsonField(tempPluPrice.units) forState:UIControlStateNormal];
                [btnUnit setTitleColor:RGBA(35, 24, 21, 255) forState:UIControlStateNormal];
                [btnUnit setBackgroundImage:[FileManager getImage:@"btn_gray1.png" type:3 withRect:btnUnit.frame] 
                                   forState:UIControlStateNormal]; //默认没有选中的背景图片
                
                //当前要显示的价格
                if (priceIndex == i) {
                    imgMark.hidden = YES;
                    [btnNum setTitle:@"0.00" forState:UIControlStateNormal];
                    [btnNum setTitleColor:RGBA(163, 163, 163, 255) forState:UIControlStateNormal];//数量为0显示灰色字体
                    [lblPrice setText:PriceFormatter(_currentPluPrice.price,JsonField(_currentPluPrice.units))]; //显示当前选中的价格和单位
                    [btnUnit setTitleColor:RGBA(255, 255, 255, 255) forState:UIControlStateNormal];
                    [btnUnit setBackgroundImage:[FileManager getImage:@"btn_blue1.png" type:3 withRect:btnUnit.frame]
                                       forState:UIControlStateNormal];//选中的背景图片
                    
                    //----------------------------查询当前菜品的当前价格是否已经被点过了----------------------------
                    NSString *numSql=[NSString stringWithFormat:@"select * from MyOrderDishes where momLocalMenu='%@' and ppCode='%@' and cast(modPrice as double) = %f and modUnits like '%%%@%%'",AppSession.currentOrderMenu.momLocalMenu,_currentPlu.ppCode,[_currentPluPrice.price floatValue],JsonField(_currentPluPrice.units)];
                    NSString *number = @"0.00";
                    if (AppSession.fullScreenType == 4) {//检测套菜有没有被点
                        numSql=[NSString stringWithFormat:@"select * from MyOrderPluPkgs where momLocalMenu='%@' and ppCode='%@' and pkgCode='%@' and cast(pkgPrice as double) = %f and pkgUnit like '%%%@%%'",AppSession.currentOrderMenu.momLocalMenu,AppSession.currentMyOrderDish.ppCode,_currentPlu.ppCode,[_currentPluPrice.price floatValue],JsonField(_currentPluPrice.units)];
                        number = [DataBaseAccess getMyOrderNumber:numSql column:@"pkgNumber"];
                    }else {
                        number = [DataBaseAccess getMyOrderNumber:numSql column:@"modNumber"];
                    }
                    MyNSLog(@"numSql=%@,number=%@",numSql,number);
                    [btnNum setTitle:number forState:UIControlStateNormal];
                    if (![number isEqualToString:@"0.00"]) {
                        imgMark.hidden = NO;
                        [btnNum setTitleColor:[UIColor redColor] forState:UIControlStateNormal];//已点单且数量不为0时显示红色字体
                    }     
                    //----------------------------END----------------------------
                }
                
                btnUnit.hidden = NO;
            }else {
                btnUnit.hidden = YES;
            }
        }
    }
    
    [self checkCookViewDisplay];
}
//检测烧法窗口是否显示
-(void) checkCookViewDisplay{
    NSString *cookTemp = @"";
    if (AppSession.fullScreenType == 2){
        cookTemp = _currentMyOrderDish.modCook;
    }else{
        //----------------------------从数据库里查询烧法信息---------------------------
        NSString *numSql=[NSString stringWithFormat:@"select * from MyOrderDishes where momLocalMenu='%@' and ppCode='%@' and cast(modPrice as double) = %f and modUnits like '%%%@%%'",AppSession.currentOrderMenu.momLocalMenu,_currentPlu.ppCode,[_currentPluPrice.price floatValue],JsonField(_currentPluPrice.units)];
        if (AppSession.fullScreenType == 4) {//检测套菜有没有被点
            numSql=[NSString stringWithFormat:@"select * from MyOrderPluPkgs where momLocalMenu='%@' and ppCode='%@' and pkgCode='%@' and cast(pkgPrice as double) = %f and pkgUnit like '%%%@%%'",AppSession.currentOrderMenu.momLocalMenu,AppSession.currentMyOrderDish.ppCode,_currentPlu.ppCode,[_currentPluPrice.price floatValue],JsonField(_currentPluPrice.units)];
            cookTemp = [DataBaseAccess getMyOrderNumber:numSql column:@"pkgCook"];//查询套餐明细的烧法
        }else {
            cookTemp = [DataBaseAccess getMyOrderNumber:numSql column:@"modCook"];//查询我的点单的烧法
        }
        if ([cookTemp isEqualToString:@"0.00"]) {
            cookTemp = @"";
        }
        MyNSLog(@"numSql=%@,cookTemp=%@",numSql, cookTemp);
        //----------------------------END----------------------------
    }
    
    if (isRemarkDisplay) {
        viewRemark.hidden = NO;
        imgDirection.image = [FileManager getImage:@"arrow_down_fullscreen.png" type:3 withRect:imgDirection.frame];
    }else {
        viewRemark.hidden = YES;
        imgDirection.image = [FileManager getImage:@"arrow_up_fullscreen.png" type:3 withRect:imgDirection.frame];
    }
    
    if (cookTemp == nil || cookTemp.length < 1) {
        viewCook.hidden = YES;
    }else {
        NSString * oldNumberString = btnNum.titleLabel.text;
        if ([oldNumberString floatValue] == 0) {
            viewCook.hidden = YES;
        }else {
            viewCook.hidden = NO;
            lblCook.text = cookTemp;
            if (isRemarkDisplay) {
                viewCook.frame = CGRectMake(viewPluName.frame.origin.x, 
                                            viewRemark.frame.origin.y+viewRemark.frame.size.height+10, 
                                            viewRemark.frame.size.width, 
                                            150);
            }else {
                viewCook.frame = CGRectMake(viewPluName.frame.origin.x, 
                                            viewPluName.frame.origin.y+viewPluName.frame.size.height+10, 
                                            viewPluName.frame.size.width, 
                                            150);
            }
        }
    }
}

#pragma mark - 左右翻页操作
//判断是否可以翻页
-(BOOL) checkIndexRange:(BOOL) nextOrPrevious{
    if (AppSession.fullScreenType == 0) {
        if (nextOrPrevious) {//下一页
            if (AppSession.commendIndex+1 < [AppSession.commendPluPlus count]){
                AppSession.commendIndex++;
                return YES;
            }else {
                return NO;
            }
        }else {//上一页
            if(AppSession.commendIndex-1 >= 0) {
                AppSession.commendIndex--;
                return YES;
            }else {
                return NO;
            }
        }
    }else if(AppSession.fullScreenType == 1){
        if (nextOrPrevious) {//下一页
            if (AppSession.pluIndex+1 < [AppSession.orderingPluPlus count]){
                AppSession.pluIndex++;
                return YES;
            }else {
                return NO;
            }
        }else {//上一页
            if(AppSession.pluIndex-1 >= 0) {
                AppSession.pluIndex--;
                return YES;
            }else {
                return NO;
            }
        }
    }else if(AppSession.fullScreenType == 2){
        if (nextOrPrevious) {//下一页
            if (AppSession.myOrderPluIndex+1 < [AppSession.myOrderDishes count]){
                AppSession.myOrderPluIndex++;
                return YES;
            }else {
                return NO;
            }
        }else {//上一页
            if(AppSession.myOrderPluIndex-1 >= 0) {
                AppSession.myOrderPluIndex--;
                return YES;
            }else {
                return NO;
            }
        }
    }else if(AppSession.fullScreenType == 3 || AppSession.fullScreenType == 4){
        if (nextOrPrevious) {//下一页
            if (AppSession.searchIndex+1 < [AppSession.searchPlus count]){
                AppSession.searchIndex++;
                return YES;
            }else {
                return NO;
            }
        }else {//上一页
            if(AppSession.searchIndex-1 >= 0) {
                AppSession.searchIndex--;
                return YES;
            }else {
                return NO;
            }
        }
    }
    return YES;
}
//下一页
-(void) nextPage{
    if ([self checkIndexRange:YES]) {
        [AppSession playMusic:VoiceFile(@"trash.caf")];
        [self refreshData];
        
        UIImageView *dishImg = (UIImageView *)[self.view viewWithTag:TagOfUIImageView];    
        CATransition *animation = [CATransition animation];
        animation.duration = animateTime;
        animation.timingFunction = UIViewAnimationCurveEaseInOut;
        animation.fillMode = kCAFillModeForwards;
        animation.type = kCATransitionPush;
        animation.subtype = kCATransitionFromRight; 
        [dishImg.layer addAnimation:animation forKey:@"animation"];
    }else {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:TranslateStr(@"提示")
                                                        message:TranslateStr(@"最后一道菜")
                                                       delegate:self 
                                              cancelButtonTitle:TranslateStr(@"确定")
                                              otherButtonTitles:nil];    
        [alert show];     
        [alert release];
    }
}
//上一页
-(void) previousPage{
    if ([self checkIndexRange:NO]) {
        [AppSession playMusic:VoiceFile(@"trash.caf")];
        [self refreshData];
        
        UIImageView *dishImg = (UIImageView *)[self.view viewWithTag:TagOfUIImageView];    
        CATransition *animation = [CATransition animation];
        animation.duration = animateTime;
        animation.timingFunction = UIViewAnimationCurveEaseInOut;
        animation.fillMode = kCAFillModeForwards;
        animation.type = kCATransitionPush;
        animation.subtype = kCATransitionFromLeft; 
        [dishImg.layer addAnimation:animation forKey:@"animation"];
    }else {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:TranslateStr(@"提示")
                                                        message:TranslateStr(@"第一道菜")
                                                       delegate:self 
                                              cancelButtonTitle:TranslateStr(@"确定")
                                              otherButtonTitles:nil];    
        [alert show];
        [alert release];
    }
}

#pragma mark - 按钮事件
-(IBAction)buttonClicked:(id)sender {
    if (AppSession.isAnimating) {
        MyNSLog(@"上一个动画还未完成...");
        return;
    }else {
        AppSession.isAnimating = YES;
    }
    [AppSession playMusic:VoiceFile(@"clicked.caf")];
    NSInteger bid = [sender tag];
    UIButton *btn = (UIButton *)[viewControl viewWithTag:bid];
    if (bid < TagOfUIButton+5) {
        MyNSLog(@"选中价格 index=%d", bid - TagOfUIButton);
        [self setPriceSelected:bid - TagOfUIButton];
    }else if (bid == TagOfUIButton + 10) {
        if ([AppSession.currentOrderMenu.momSendErr isEqualToString:@"1"]) {
            postN(@"SysMsg_RootView_AlertErrMsg");
            return;
        }
        if (AppSession.fullScreenType == 4 && [_currentPlu.ppType isEqualToString:@"3"]) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:TranslateStr(@"提示")
                                                            message:TranslateStr(@"套餐本身不能作为套餐的明细")
                                                           delegate:self 
                                                  cancelButtonTitle:TranslateStr(@"确定")
                                                  otherButtonTitles:nil];    
            [alert show];
            [alert release];
            AppSession.isAnimating = NO;
            return;
        }
        
        NSString * oldNumber = btnNum.titleLabel.text;
        PopoverPickerviewController *ppc = [[PopoverPickerviewController alloc] initWithNibName:@"PopoverPickerviewController" number:oldNumber];
        [ppc PopOverFromRect:btn.frame inView:viewControl directions:UIPopoverArrowDirectionDown];
        [ppc release];
    }else if (bid == TagOfUIButton + 11) {
        MyNSLog(@"点单");
        if ([AppSession.currentOrderMenu.momSendErr isEqualToString:@"1"]) {
            postN(@"SysMsg_RootView_AlertErrMsg");
            return;
        }
        if (AppSession.fullScreenType == 4 && [_currentPlu.ppType isEqualToString:@"3"]) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:TranslateStr(@"提示")
                                                            message:TranslateStr(@"套餐本身不能作为套餐的明细")
                                                           delegate:self 
                                                  cancelButtonTitle:TranslateStr(@"确定")
                                                  otherButtonTitles:nil];    
            [alert show];
            [alert release];
            AppSession.isAnimating = NO;
            return;
        }
        
        NSString * oldNumberString = btnNum.titleLabel.text;
        NSInteger numBeforeDot = [oldNumberString intValue];//整数
        NSString * newNumberString = [oldNumberString replaceFrom:[NSString stringWithFormat:@"%d.",numBeforeDot] To:[NSString stringWithFormat:@"%d.",numBeforeDot+1]];//计算出新的数量       
        
        NSDictionary *infos = [NSDictionary dictionaryWithObjectsAndKeys:newNumberString, @"number", nil];
        postNWithInfos(@"SysMsg_FullScreenMode_changeBtnNum", self, infos);
    }else if (bid == TagOfUIButton + 12) {
        MyNSLog(@"烧法");    
        if ([AppSession.currentOrderMenu.momSendErr isEqualToString:@"1"]) {
            postN(@"SysMsg_RootView_AlertErrMsg");
            return;
        }
         
        NSString * oldNumberString = btnNum.titleLabel.text;
        if ([oldNumberString floatValue] == 0 && AppSession.fullScreenType != 2) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:TranslateStr(@"提示")
                                                            message:TranslateStr(@"先点菜")
                                                           delegate:self 
                                                  cancelButtonTitle:TranslateStr(@"返回")
                                                  otherButtonTitles:nil];
            [alert show];
            [alert release];
        }else {//弹出烧法窗口
            if (AppSession.fullScreenType == 2) {
                NSDictionary *infos = [NSDictionary dictionaryWithObjectsAndKeys:_currentMyOrderDish, @"pluObj",nil];
                postNWithInfos(@"SysMsg_RootView_PopOutSelectCookWithInfo", nil, infos);
            }else {
                for (int p=0; p<_currentPlu.ppMyOrderDishes.count; p++) {
                    MyOrderDishes *tempMyOrderDish = (MyOrderDishes *)[_currentPlu.ppMyOrderDishes objectAtIndex:p];
                    if ([_currentPluPrice.price floatValue] == [tempMyOrderDish.modPrice floatValue]) {
                        NSDictionary *infos = [NSDictionary dictionaryWithObjectsAndKeys:tempMyOrderDish, @"pluObj",nil];
                        postNWithInfos(@"SysMsg_RootView_PopOutSelectCookWithInfo", nil, infos);
                        break;
                    }
                }
            }
        }
    }else if (bid == TagOfUIButton + 13) {
        MyNSLog(@"返回点菜...");        
        NSString *SysMsg = [NSString stringWithFormat:@"SysMsg_MessageBoxBaseView_%@", self.nibName];
        postN(SysMsg);//关闭弹出窗口
    }
    AppSession.isAnimating = NO;
}
//修改已点单数量
-(void) changeBtnNum:(NSNotification *)note{
	NSDictionary *infos = [note userInfo];
	if ([infos objectForKey:@"number"]) {
        NSString * numberString = (NSString *)[infos objectForKey:@"number"];
        //修改界面上的数量
        if ([numberString floatValue] == 0) {
            imgMark.hidden = YES;
            [btnNum setTitle:@"0.00" forState:UIControlStateNormal];
            [btnNum setTitleColor:RGBA(163, 163, 163, 255) forState:UIControlStateNormal]; 
        }else {
            imgMark.hidden = NO;
            [btnNum setTitle:numberString forState:UIControlStateNormal];
            [btnNum setTitleColor:[UIColor redColor] forState:UIControlStateNormal]; 
        }
        
        //修改数据库里的数量
        if (AppSession.fullScreenType == 2) {//已点单改数量
            [_currentMyOrderDish updateNumber:numberString];//改数量
        }else {
            if (AppSession.fullScreenType == 4) {//套餐明细 添加+修改
                BOOL isNeedInsert = YES;
                for (int p=0; p<AppSession.myOrderPluPkgs.count; p++) {
                    MyOrderPluPkgs *tempMyOrderPkg = (MyOrderPluPkgs *)[AppSession.myOrderPluPkgs objectAtIndex:p];
                    if ([_currentPluPrice.price floatValue] == [tempMyOrderPkg.pkgPrice floatValue]) {
                        isNeedInsert = NO;
                        [tempMyOrderPkg updateNumber:numberString];//改数量
                        break;
                    }
                }
                
                if (isNeedInsert) {//添加套餐明细
                    [_currentPlu insertNewMyOrderPkg:numberString price:_currentPluPrice.price units:_currentPluPrice.units];
                }
            }else {//普通菜品 添加+修改
                BOOL isNeedInsert = YES;
                for (int p=0; p<_currentPlu.ppMyOrderDishes.count; p++) {
                    MyOrderDishes *tempMyOrderDish = (MyOrderDishes *)[_currentPlu.ppMyOrderDishes objectAtIndex:p];
                    if ([_currentPluPrice.price floatValue] == [tempMyOrderDish.modPrice floatValue]) {
                        isNeedInsert = NO;
                        [tempMyOrderDish updateNumber:numberString];//改数量
                        break;
                    }
                }
                
                if (isNeedInsert) {
                    [_currentPlu insertNewMyOrderDish:numberString price:_currentPluPrice.price units:_currentPluPrice.units];
                }
            }
        }
        [self checkCookViewDisplay];
    }
    
    //发送修改主界面的消息
    if (AppSession.fullScreenType == 0) {//刷新推荐菜界面
        MyNSLog(@"刷新推荐菜界面");
        postN(@"SysMsg_GuestCommendView_refreshCommendView");
    }else if (AppSession.fullScreenType == 1) {//刷新客人点菜界面
        MyNSLog(@"刷新客人点菜界面");
        postN(@"SysMsg_GuestNineModeView_refreshGuestNineModeView");
    }else if(AppSession.fullScreenType == 2){//刷新我的点单图片模式界面
        MyNSLog(@"刷新我的点单图片模式界面");            
        postN(@"SysMsg_MyOrderNineModeView_refreshNineMode");
        _currentMyOrderDish = (MyOrderDishes *)[AppSession.myOrderDishes objectAtIndex:AppSession.myOrderPluIndex];//初始化myOrderDishes后要重新赋值
    }else if(AppSession.fullScreenType== 3){//查询界面
        MyNSLog(@"查询界面");
        postN(@"SysMsg_GuestNineModeView_refreshGuestNineModeView");
        postN(@"SysMsg_GuestCommendView_refreshCommendView");
    }else if(AppSession.fullScreenType == 4){//刷新套餐明细
        MyNSLog(@"刷新套餐明细...");
        postN(@"SysMsg_PluPackageManagement_refreshPluPkgTable");
    }
}
#pragma mark - View lifecycle
- (void)viewDidLoad{
    addNObserver(@selector(changeBtnNum:), @"SysMsg_FullScreenMode_changeBtnNum");
    addNObserver(@selector(checkCookViewDisplay), @"SysMsg_FullScreenMode_checkCookViewDisplay");
    [super viewDidLoad];
}
- (void)viewDidUnload{
    [super viewDidUnload];
}
-(void)dealloc{
    MyNSLog(@"FullScreenMode is deallocing...");
    removeNObserver(@"SysMsg_FullScreenMode_changeBtnNum");
    removeNObserver(@"SysMsg_FullScreenMode_checkCookViewDisplay");
    [super dealloc];
}
- (void)didReceiveMemoryWarning{
    [super didReceiveMemoryWarning];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
	return (interfaceOrientation == UIInterfaceOrientationLandscapeRight || interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}
@end